#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 100010;

int n, m;
int h[N], cnt;

// x 的左儿子下标为2 * x，右儿子下标为2 * x + 1 

void down(int u) {
	int t = u, l = 2 * u, r = l + 1; 
	if (l <= cnt && h[l] < h[t]) t =l;
	if (r <= cnt && h[r] < h[t]) t = r;
	if (t != u) {
		swap(h[t], h[u]);
		down(t);
	}
} 


int main() {
	cin >> n >> m;
	
    cnt = n;
	for (int i = 1; i <= n; i ++ ) scanf("%d", h + i);
	for (int i = n / 2; i; i -- ) down(i);
	
	while (m -- ) {
		printf("%d ", h[1]);
		h[1] = h[cnt];
		cnt --;
		down(1);
	}
	
	return 0;
}